导航菜单
首页 >  template package  > Template parameters and template arguments

Template parameters and template arguments

 C++Compiler supportFreestanding and hostedLanguageStandard libraryStandard library headersNamed requirementsFeature test macros (C++20)Language support libraryConcepts library (C++20)Metaprogramming library (C++11)Diagnostics libraryGeneral utilities libraryStrings libraryContainers libraryIterators libraryRanges library (C++20)Algorithms libraryNumerics libraryLocalizations libraryInput/output libraryFilesystem library (C++17)Regular expressions library (C++11)Concurrency support library (C++11)Execution support library (C++26)Technical specificationsSymbols indexExternal libraries[edit] C++ languageGeneral topicsPreprocessorCommentsKeywordsEscape sequencesFlow controlConditional execution statementsifswitchIteration statements (loops)forrange-for (C++11)whiledo-whileJump statementscontinue - breakgoto - returnFunctionsFunction declarationLambda function expressioninline specifierDynamic exception specifications (until C++17*)noexcept specifier (C++11)Exceptionsthrow-expressiontry blockhandlerNamespacesNamespace declaration  Namespace aliasesTypesFundamental typesEnumeration typesFunction typesClass/struct typesUnion typesSpecifiersconst/volatiledecltype (C++11)auto (C++11)constexpr (C++11)consteval (C++20)constinit (C++20)Storage duration specifiersInitializationDefault-initializationValue-initializationZero-initializationCopy-initializationDirect-initializationAggregate initializationList-initialization (C++11)  Constant initializationReference initializationExpressionsValue categoriesOrder of evaluationOperatorsOperator precedenceAlternative representationsLiteralsBoolean - Integer - Floating-pointCharacter - String - nullptr (C++11)User-defined (C++11)UtilitiesAttributes (C++11)Typestypedef declarationType alias declaration (C++11)CastsImplicit conversionsstatic_castconst_castExplicit conversionsdynamic_castreinterpret_castMemory allocationnew expressiondelete expressionClassesClass declarationConstructorsthis pointerAccess specifiersfriend specifierClass-specific function propertiesVirtual functionoverride specifier (C++11)  final specifier (C++11)explicit (C++11)staticSpecial member functionsDefault constructorCopy constructorMove constructor (C++11)Copy assignmentMove assignment (C++11)DestructorTemplatesClass templateFunction templateTemplate specializationParameter packs (C++11)MiscellaneousInline assemblyHistory of C++[edit] DeclarationsOverviewDeclaration syntaxdecl-specifier-seqDeclaratorConflicting declarationsSpecifierstypedefinlinevirtual function specifierexplicit function specifierfriendconstexpr(C++11)consteval(C++20)constinit(C++20)Storage class specifiersTranslation-unit-local (C++20)class/structunionenumdecltype(C++11)auto(C++11)alignas(C++11)constvolatilePack indexing specifier (C++26)    Elaborated type specifierAttributes (C++11)DeclaratorsReferencePointerArrayBlock declarationsSimple-declaration→Structured binding declaration (C++17)Alias declaration (C++11)Namespace alias definitionusing declarationusing directivestatic_assert declaration (C++11)asm declarationOpaque enum declaration (C++11)Other declarationsNamespace definitionFunction declarationClass template declarationFunction template declarationExplicit template instantiation (C++11)Explicit template specializationLinkage specificationAttribute declaration (C++11)Empty declaration[edit]  ExpressionsGeneralValue categoriesOrder of evaluationConstant expressionsPrimary expressionsLambda expressions (C++11)Requires expressions (C++20)Pack indexing expression (C++26)Potentially-evaluated expressionsLiteralsInteger literalsFloating-point literalsBoolean literalsCharacter literalsEscape sequencesString literalsNull pointer literal (C++11)User-defined literal (C++11)OperatorsAssignment operatorsIncrement and decrementArithmetic operatorsLogical operatorsComparison operatorsMember access operatorsOther operatorsnew-expressiondelete-expressionthrow-expressionalignofsizeofsizeof... (C++11)typeidnoexcept (C++11)Fold expressions (C++17)Alternative representations of operatorsPrecedence and associativityOperator overloadingDefault comparisons (C++20)ConversionsImplicit conversionsExplicit conversionsUsual arithmetic conversions    User-defined conversionconst_caststatic_castdynamic_castreinterpret_cast[edit]  Templates Parameters and argumentsClass templatesFunction templatesClass member templatesVariable templates (C++14) Template argument deductionClass template argument deduction (C++17)Explicit (full) specializationPartial specializationDependent namesParameter packs (C++11)    sizeof... (C++11)    Fold expressions (C++17)    Pack indexing (C++26) SFINAEConstraints and concepts (C++20)Requires expression (C++20) [edit] Contents1 Template parameters1.1 Non-type template parameter1.2 Type template parameter1.3 Template template parameter1.4 Name resolution for template parameters2 Template arguments2.1 Template non-type arguments2.2 Template type arguments2.3 Template template arguments2.4 Default template arguments2.5 Template argument equivalence3 Notes4 Examples5 Defect reports[edit] Template parameters

Every template is parameterized by one or more template parameters, indicated in the parameter-list of the template declaration syntax:

template declaration (1) template requires constraint declaration (2) (since C++20)

Each parameter in parameter-list may be:

a non-type template parameter; a type template parameter; a template template parameter.[edit] Non-type template parameter type name (optional) (1) type name (optional) = default (2) type ... name (optional) (3) (since C++11)1) A non-type template parameter.2) A non-type template parameter with a default template argument.3) A non-type template parameter pack. type - one of the following types: a structural type (see below) a type that contains a placeholder type(since C++17) a placeholder for a deduced class type(since C++20) name - the name of the non-type template parameter default - the default template argument

A structural type is one of the following types (optionally cv-qualified, the qualifiers are ignored):

lvalue reference type (to object or to function); an integral type; a pointer type (to object or to function); a pointer to member type (to member object or to member function); an enumeration type; std::nullptr_t;(since C++11) a floating-point type; a lambda closure type whose lambda expression has no capture; a non-closure literal class type with the following properties: all base classes and non-static data members are public and non-mutable and the types of all base classes and non-static data members are structural types or (possibly multi-dimensional) array thereof.(since C++20)

Array and function types may be written in a template declaration, but they are automatically replaced by pointer to object and pointer to function as appropriate.

When the name of a non-type template parameter is used in an expression within the body of the class template, it is an unmodifiable prvalue unless its type was an lvalue reference type, or unless its type is a class type(since C++20).

A template parameter of the form class Foo is not an unnamed non-type template parameter of type Foo, even if otherwise class Foo is an elaborated type specifier and class Foo x; declares x to be of type Foo.

An identifier that names a non-type template parameter of class type T denotes a static storage duration object of type const T, called a template parameter object, which is template-argument-equivalent to the corresponding template argument after it has been converted to the type of the template parameter. No two template parameter objects are template-argument-equivalent.

struct A{friend bool operator==(const A&, const A&) = default;}; templatevoid f(){&a;// OKconst A& ra = a, &rb = a; // Both bound to the same template parameter objectassert(&ra == &rb);// passes}(since C++20)[edit] Type template parameter type-parameter-key name (optional) (1) type-parameter-key name (optional) = default (2) type-parameter-key ... name (optional) (3) (since C++11) type-constraint name (optional) (4) (since C++20) type-constraint name (optional) = default (5) (since C++20) type-constraint ... name (optional) (6) (since C++20) type-parameter-key - either typename or class. There is no difference between these keywords in a type template parameter declaration type-constraint - either the name of a concept or the name of a concept followed by a list of template arguments (in angle brackets). Either way, the concept name may be optionally qualified name - the name of the type template parameter default - the default template argument1) A type template parameter without a default.templateclass My_vector { /* ... */ };2) A type template parameter with a default.templatestruct My_op_functor { /* ... */ };3) A type template parameter pack.templateclass My_tuple { /* ... */ };4) A constrained type template parameter without a default.templateclass My_constrained_vector { /* ... */ };5) A constrained type template parameter with a default.templateclass My_constrained_op_functor { /* ... */ };6) A constrained type template parameter pack.templateclass My_constrained_tuple { /* ... */ };

The name of the parameter is optional:

// Declarations of the templates shown above:templateclass My_vector;templatestruct My_op_functor;templateclass My_tuple;

In the body of the template declaration, the name of a type parameter is a typedef-name which aliases the type supplied when the template is instantiated.

Each constrained parameter P whose type-constraint is Q designating the concept C introduces a constraint-expression E according to the following rules:

if Q is C (without an argument list), if P is not a parameter pack, E is simply C

otherwise, P is a parameter pack, E is a fold-expression (C

&& ...) if Q is C, then E is C or (C && ...), respectively.templateconcept C1 = true;template // variadic conceptconcept C2 = true;templateconcept C3 = true; template struct s1; // constraint-expression is C1template struct s2; // constraint-expression is (C1 && ...)template struct s3; // constraint-expression is (C2 && ...)templatestruct s4; // constraint-expression is C3template struct s5; // constraint-expression is (C3 && ...)(since C++20)[edit] Template template parameter template type-parameter-key name (optional) (1) template type-parameter-key name (optional) = default (2) template type-parameter-key ... name (optional) (3) (since C++11) type-parameter-key - class or typename(since C++17)1) A template template parameter with an optional name.2) A template template parameter with an optional name and a default.3) A template template parameter pack with an optional name.

In the body of the template declaration, the name of this parameter is a template-name (and needs arguments to be instantiated).

templateclass my_array {}; // two type template parameters and one template template parameter:templateclass Map{C key;C value;};[edit] Name resolution for template parameters

The name of a template parameter is not allowed to be redeclared within its scope (including nested scopes). A template parameter is not allowed to have the same name as the template name.

templateclass Y{int T; // error: template parameter redeclaredvoid f(){char T; // error: template parameter redeclared}}; templateclass X; // error: template parameter redeclared

In the definition of a member of a class template that appears outside of the class template definition, the name of a member of the class template hides the name of a template parameter of any enclosing class templates, but not a template parameter of the member if the member is a class or function template.

templatestruct A{struct B {};typedef void C;void f(); templatevoid g(U);}; templatevoid A::f(){B b; // A's B, not the template parameter} templatetemplatevoid A::g(C){B b; // A's B, not the template parameterC c; // the template parameter C, not A's C}

In the definition of a member of a class template that appears outside of the namespace containing the class template definition, the name of a template parameter hides the name of a member of this namespace.

namespace N{class C {}; templateclass B{void f(T);};} templatevoid N::B::f(C){C b; // C is the template parameter, not N::C}

In the definition of a class template or in the definition of a member of such a template that appears outside of the template definition, for each non-dependent base class, if the name of the base class or the name of a member of the base class is the same as the name of a template parameter, the base class name or member name hides the template parameter name.

struct A{struct B {};int C;int Y;}; templatestruct X : A{B b; // A's BC b; // error: A's C isn't a type name};[edit] Template arguments

In order for a template to be instantiated, every template parameter (type, non-type, or template) must be replaced by a corresponding template argument. For class templates, the arguments are either explicitly provided, deduced from the initializer, (since C++17) or defaulted. For function templates, the arguments are explicitly provided, deduced from the context, or defaulted.

If an argument can be interpreted as both a type-id and an expression, it is always interpreted as a type-id, even if the corresponding template parameter is non-type:

templatevoid f(); // #1 templatevoid f(); // #2 void g(){f(); // "int()" is both a type and an expression,// calls #1 because it is interpreted as a type}[edit] Template non-type arguments

The template argument that can be used with a non-type template parameter can be any manifestly constant-evaluated expression.

(until C++11)

The template argument that can be used with a non-type template parameter can be any initializer clause. If the initializer clause is an expression, it must be manifestly constant-evaluated.

(since C++11)

Given the type of the non-type template parameter declaration as T.

If T contains a placeholder type, or is a placeholder for a deduced class type, the type of the template parameter is the type deduced for the variable x in the invented declaration T x = E;, where E is the template argument provided for the parameter.

If a deduced parameter type is not a structural type, the program is ill-formed.

templatestruct B { /* ... */ }; B b1;// OK: non-type template parameter type is intB b2; // OK: non-type template parameter type is charB b3; // error (until C++20): non-type template parameter type cannot be double // C++20 deduced class type placeholder, class template arguments are deduced at the// call sitetemplatevoid f(); f();

For non-type template parameter packs whose type uses a placeholder type, the type is independently deduced for each template argument and need not match:

templatestruct C {}; C x; // OK(since C++17)

The value of a non-type template parameter P of (possibly deduced)(since C++17) type T is determined from its template argument A as follows:

If A is a converted constant expression of type T, the value of P is A (as converted). Otherwise, the program is ill-formed.(until C++11) If A is an expression: If A is a converted constant expression of type T, the value of P is A (as converted). Otherwise, the program is ill-formed. Otherwise (A is a braced-enclosed initializer list), a temporary variable constexpr T v = A; is introduced. The value of P is that of v. The lifetime of v ends immediately after initializing it.(since C++11)(until C++20) If T is not a class type and A is an expression: If A is a converted constant expression of type T, the value of P is A (as converted). Otherwise, the program is ill-formed. Otherwise (T is a class type or A is a braced-enclosed initializer list), a temporary variable constexpr T v = A; is introduced.If T is a class type, a template parameter object exists (which is also denoted by P). P is copy-initialized from an unspecified candidate initializer that is template-argument-equivalent to v. The lifetime of v ends immediately after initializing it and P. If the initialization of P satisfies any of the following conditions, the program is ill-formed: The initialization would be ill-formed. The full-expression of an invented declarator-initializer sequence for the initialization would not be a constant expression when interpreted as a manifestly constant-evaluated expression. The initialization would cause P to not be template-argument-equivalent to v. Otherwise, the value of P is that of v.(since C++20)templatestruct C { /* ... */ }; C c1; // OK templatestruct B { /* ... */ }; struct J1{J1* self = this;}; B j1; // error: initialization of the template parameter object//is not a constant expression struct J2{J2 *self = this;constexpr J2() {}constexpr J2(const J2&) {}}; B j2; // error: the template parameter object is not//template-argument-equivalent to introduced temporary

The following limitations apply when instantiating templates that have non-type template parameters:

For integral and arithmetic types, the template argument provided during instantiation must be a converted constant expression of the template parameter's type (so certain implicit conversion applies). For pointers to objects, the template arguments have to designate the address of a complete object with static storage duration and a linkage (either internal or external), or a constant expression that evaluates to the appropriate null pointer or std::nullptr_t(since C++11) value. For pointers to functions, the valid arguments are pointers to functions with linkage (or constant expressions that evaluate to null pointer values). For lvalue reference parameters, the argument provided at instantiation cannot be a temporary, an unnamed lvalue, or a named lvalue with no linkage (in other words, the argument must have linkage). For pointers to members, the argument has to be a pointer to member expressed as &Class::Member or a constant expression that evaluates to null pointer or std::nullptr_t(since C++11) value.

In particular, this implies that string literals, addresses of array elements, and addresses of non-static members cannot be used as template arguments to instantiate templates whose corresponding non-type template parameters are pointers to objects.

(until C++17)

Non-type template parameters of reference or pointer type and non-static data members of reference or pointer type in a non-type template parameter of class type and its subobjects(since C++20) cannot refer to/be the address of

a temporary object (including one created during reference initialization); a string literal; the result of typeid; the predefined variable __func__; or a subobject (including non-static class member, base subobject, or array element) of one of the above(since C++20).(since C++17)templatestruct X {}; int ai[10];X xi; // OK: array to pointer conversion and cv-qualification conversion struct Y {}; templatestruct Z {}; Y y;Z z;// OK: no conversion templatestruct W {}; int b[5];W w;// OK: no conversion void f(char);void f(int); templatestruct A {}; A a; // OK: overload resolution selects f(int)templateclass X {}; X x1; // error: string literal as template-argument templateclass X {}; int a[10]; struct S{int m;static int s;} s; X x3; // error (until C++20): address of array elementX x4; // error (until C++20): address of non-static memberX x5; // OK: address of static memberX x6; // OK: address of static member templatestruct B {}; B b2; // error: temporary would be required for template argumentint c = 1;B b1; // OK[edit] Template type arguments

A template argument for a type template parameter must be a type-id, which may name an incomplete type:

templateclass X {}; // class template struct A;// incomplete typetypedef struct {} B; // type alias to an unnamed type int main(){X x1; // OK: 'A' names a typeX x2; // OK: 'A*' names a typeX x3; // OK: 'B' names a type}[edit] Template template arguments

A template argument for a template template parameter must be an id-expression which names a class template or a template alias.

When the argument is a class template, only the primary template is considered when matching the parameter. The partial specializations, if any, are only considered when a specialization based on this template template parameter happens to be instantiated.

template // primary templateclass A { int x; }; template // partial specializationclass A { long x; }; // class template with a template template parameter Vtemplateclass C{V y; // uses the primary templateV z; // uses the partial specialization}; C c; // c.y.x has type int, c.z.x has type long

To match a template template argument A to a template template parameter P, P must be at least as specialized as A (see below). If P's parameter list includes a parameter pack, zero or more template parameters (or parameter packs) from A's template parameter list are matched by it.(since C++11)

Formally, a template template-parameter P is at least as specialized as a template template argument A if, given the following rewrite to two function templates, the function template corresponding to P is at least as specialized as the function template corresponding to A according to the partial ordering rules for function templates. Given an invented class template X with the template parameter list of A (including default arguments):

Each of the two function templates has the same template parameters, respectively, as P or A. Each function template has a single function parameter whose type is a specialization of X with template arguments corresponding to the template parameters from the respective function template where, for each template parameter PP in the template parameter list of the function template, a corresponding template argument AA is formed. If PP declares a parameter pack, then AA is the pack expansion PP...; otherwise,(since C++11) AA is the id-expression PP.

If the rewrite produces an invalid type, then P is not at least as specialized as A.

templatestruct eval; // primary template templatestruct eval {}; // partial specialization of eval template struct A;template struct B;template struct C;template struct D;template struct E; eval eA;// OK: matches partial specialization of evaleval eB; // OK: matches partial specialization of evaleval eC; // error: C does not match TT in partial specialization// because TT's first parameter is a// type template parameter, while 17 does not name a typeeval eD;// error: D does not match TT in partial specialization// because TT's second parameter is a// type parameter pack, while 17 does not name a typeeval eE; // error: E does not match TT in partial specialization// because E's third (default) parameter is a non-type

Before the adoption of P0522R0, each of the template parameters of A must match corresponding template parameters of P exactly. This hinders many reasonable template argument from being accepted.

Although it was pointed out very early (CWG#150), by the time it was resolved, the changes were applied to the C++17 working paper and the resolution became a de facto C++17 feature. Many compilers disable it by default:

GCC disables it in all language modes prior to C++17 by default, it can only be enabled by setting a compiler flag in these modes. Clang disables it in all language modes by default, it can only be enabled by setting a compiler flag. Microsoft Visual Studio treats it as a normal C++17 feature and only enables it in C++17 and later language modes (i.e. no support in C++14 language mode, which is the default mode).template class A { /* ... */ };template class B { /* ... */ };template class C { /* ... */ }; template class X { /* ... */ };X xa; // OKX xb; // OK after P0522R0 // Error earlier: not an exact matchX xc; // OK after P0522R0 // Error earlier: not an exact match template class Y { /* ... */ };Y ya; // OKY yb; // OKY yc; // OK template class D { /* ... */ };// note: C++17template class Z { /* ... */ };Z zd; // OK after P0522R0: the template parameter // is more specialized than the template argument template struct SI { /* ... */ };template void FA(); // note: C++17FA(); // Error[edit] Default template arguments

Default template arguments are specified in the parameter lists after the = sign. Defaults can be specified for any kind of template parameter (type, non-type, or template), but not to parameter packs(since C++11).

If the default is specified for a template parameter of a primary class template, primary variable template,(since C++14) or alias template, each subsequent template parameter must have a default argument, except the very last one may be a template parameter pack(since C++11). In a function template, there are no restrictions on the parameters that follow a default, and a parameter pack may be followed by more type parameters only if they have defaults or can be deduced from the function arguments(since C++11).

Default parameters are not allowed

in the out-of-class definition of a member of a class template (they have to be provided in the declaration inside the class body). Note that member templates of non-template classes can use default parameters in their out-of-class definitions (see GCC bug 53856) in friend class template declarations in any function template declaration or definition(until C++11)

On a friend function template declaration, default template arguments are allowed only if the declaration is a definition, and no other declarations of this function appear in this translation unit.

(since C++11)

Default template arguments that appear in the declarations are merged similarly to default function arguments:

template class A;template class A; // the above is the same as the following:template class A;

But the same parameter cannot be given default arguments twice in the same scope:

template class X;template class X {}; // error

When parsing a default template argument for a non-type template parameter, the first non-nested > is taken as the end of the template parameter list rather than a greater-than operator:

template 4>// syntax errorclass X { /* ... */ }; template 4)> // OKclass Y { /* ... */ };

The template parameter lists of template template parameters can have their own default arguments, which are only in effect where the template template parameter itself is in scope:

// class template, with a type template parameter with a defaulttemplatestruct B {}; // template template parameter T has a parameter list, which// consists of one type template parameter with a defaulttemplatestruct A{void f();void g();}; // out-of-body member function template definitions templatevoid A::f(){T t; // error: TT has no default in scope} templatevoid A::g(){T t; // OK: t is T}

Member access for the names used in a default template parameter is checked at the declaration, not at the point of use:

class B {}; templateclass C{protected:typedef T TT;}; templateclass D: public U {}; D* d; // error: C::TT is protected

The default template argument is implicitly instantiated when the value of that default argument is needed, except if the template is used to name a function:

templatestruct S {}; S* p; // The default argument for U is instantiated at this point// the type of p is S*(since C++14)[edit] Template argument equivalence

Template argument equivalence is used to determine whether two template identifiers are same.

Two values are template-argument-equivalent if they are of the same type and any of the following conditions is satisfied:

They are of integral or enumeration type and their values are the same. They are of pointer type and they have the same pointer value. They are of pointer-to-member type and they refer to the same class member or are both the null member pointer value. They are of lvalue reference type and they refer to the same object or function. They are of type std::nullptr_t.(since C++11) They are of floating-point type and their values are identical. They are of array type (in which case the arrays must be member objects of some class/union) and their corresponding elements are template-argument-equivalent. They are of union type and either they both have no active member or they have the same active member and their active members are template-argument-equivalent. They are of non-union class type and their corresponding direct subobjects and reference members are template-argument-equivalent.(since C++20)[edit] Notes

In template parameters, type constraints could be used for both type and non-type parameters, depending on whether auto is present.

templateconcept C = true; templatestruct S{}; S s;

(since C++20)Feature-test macroValueStdFeature__cpp_nontype_template_parameter_auto201606L(C++17)Declaring non-type template parameters with auto__cpp_template_template_args201611L(c++17)(DR)Matching of template template-arguments__cpp_nontype_template_args201411L(C++17)Allow constant evaluation for all non-type template arguments201911L(C++20)Class types and floating-point types in non-type template parameters[edit] ExamplesRun this code#include #include #include  // simple non-type template parametertemplatestruct S { int a[N]; }; templatestruct S2 {}; // complicated non-type exampletemplatestruct Complicated{// calls the function selected at compile time// and stores the result in the array selected at compile timevoid foo(char base){ra[4] = pf(c - base);}}; // S2 s2;// error: string literal cannot be usedchar okay[] = "okay"; // static object with linkage// S2 s3; // error: array element has no linkageS2 s4; // works int a[5];int f(int n) { return n; } // C++20: NTTP can be a literal class typetemplateconstexprauto sum() { return std::accumulate(arr.cbegin(), arr.cend(), 0); } // C++20: class template arguments are deduced at the call sitestatic_assert(sum() == 31.0);// C++20: NTTP argument deduction and CTADstatic_assert(sum() == 28); int main(){S s; // s.a is an array of 10 ints.a[9] = 4; Complicated c;c.foo('0'); std::cout

相关推荐: